package com.nchu.student_score.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nchu.student_score.mapper.ProfessionMapper;
import com.nchu.student_score.model.Profession;
import com.nchu.student_score.service.ProfessionService;
import com.nchu.student_score.util.Define;
import com.nchu.student_score.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProfessionServiceImpl implements ProfessionService {
    @Autowired
    ProfessionMapper professionMapper;

    @Override
    public ResultVo<Profession> insertOne(Profession insert) {
        ResultVo<Profession> resultVo = new ResultVo<>();
        Profession profession = professionMapper.selectByPrimaryKey(insert.getProfessionId());

        if(profession != null){
            resultVo.setStatus(1);
            resultVo.setMessage("该专业编号已存在!");
        }
        else{
            professionMapper.insert(insert);
            resultVo.setStatus(0);
            resultVo.setMessage("新增专业成功!");
        }

        return resultVo;
    }

    @Override
    public ResultVo<Profession> updateOne(Profession profession) {
        ResultVo<Profession> resultVo = new ResultVo<>();
        professionMapper.updateByPrimaryKey(profession);
        resultVo.setStatus(0);
        resultVo.setMessage("修改成功!");
        return resultVo;
    }

    @Override
    public PageInfo<Profession> getProfessionList(Profession profession, Integer currPage) {
        if(currPage == null) {
            currPage = 1;
        }

        PageHelper.startPage(currPage, Define.PAGE_SIZE);
        PageInfo<Profession> pageInfo;

        if(profession.getProfessionName().trim().length() == 0 && profession.getAcademyId() == null){
            pageInfo = new PageInfo<>(professionMapper.selectAllResult());
        }
        else{
            pageInfo = new PageInfo<>(professionMapper.findProfession(profession));
        }

        return pageInfo;
    }

    @Override
    public ResultVo<Profession> getProfession(Integer academyId) {
        ResultVo<Profession> resultVo = new ResultVo<>();
        Profession profession = new Profession();
        profession.setAcademyId(academyId);
        resultVo.setList(professionMapper.findProfession(profession));
        return resultVo;
    }
}
